package com.jdcjk.project.car.view;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.bigkoo.pickerview.TimePickerView;
import com.jdcjk.R;
import com.jdcjk.util.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.jdcjk.R.id.day_before_yesterday_rb;
import static com.jdcjk.R.id.end_time_et;
import static com.jdcjk.R.id.start_time_et;
import static com.jdcjk.R.id.today_rb;
import static com.jdcjk.R.id.user_defined_rb;
import static com.jdcjk.R.id.yesterday_rb;

/**
 * Created by Administrator on 2016/12/22 0022.
 */

public class ChoiceTimeDialog extends DialogFragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private final String TAG = ChoiceTimeDialog.class.getSimpleName();
    private RadioGroup radioGroup;

    //开始时间EditText
    private EditText startTime_editText;
    //停止时间EditText
    private EditText endTime_editText;
    private View.OnClickListener onClickListener;
    //时间选择器
    private TimePickerView timePickerView;
    private boolean isStart = false;
    private String startTime;
    private String endTime;
    private long todayTimeInLong;
    //时间格式
    private SimpleDateFormat simpleDataFormat;

    public ChoiceTimeDialog() {
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View viewContent = inflater.inflate(R.layout.dialog_choice_time, container, false);
        //去掉dialog默认标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        radioGroup = (RadioGroup) viewContent.findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(this);
        startTime_editText = (EditText) viewContent.findViewById(start_time_et);
        startTime_editText.setOnClickListener(this);
        endTime_editText = (EditText) viewContent.findViewById(end_time_et);
        endTime_editText.setOnClickListener(this);
        //设置输入框不可点击，只有自定义时才可以点击
        startTime_editText.setEnabled(false);
        endTime_editText.setEnabled(false);
        startTime_editText.setInputType(InputType.TYPE_NULL); //不显示软键盘
        endTime_editText.setInputType(InputType.TYPE_NULL); //不显示软键盘
        //确定 取消按钮监听
        viewContent.findViewById(R.id.dialog_choice_time_determine_btn).setOnClickListener(onClickListener);
        viewContent.findViewById(R.id.dialog_choice_time_cancle_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChoiceTimeDialog.this.dismiss();
            }
        });
        //设置时间选择器
        timePickerView = new TimePickerView(getActivity(), TimePickerView.Type.ALL);
        timePickerView.setTime(new Date());
        todayTimeInLong = TimeUtils.getCurrentTimeInLong();
        timePickerView.setCyclic(false);
        timePickerView.setCancelable(true);
        //初始化时间格式
        simpleDataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //时间选择后回调
        timePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                ChoiceTimeDialog.this.getDialog().show();
                String time = TimeUtils.getTime(date, simpleDataFormat);
                if (isStart) {
                    startTime = time;
                    startTime_editText.setText(startTime != null ? startTime : "");
                } else {
                    endTime = time;
                    endTime_editText.setText(endTime != null ? endTime : "");
                }
            }
        });
        //默认为今天
        startTime = TimeUtils.getTime(todayTimeInLong, TimeUtils.DATE_FORMAT_DATE) + " 00:00";
        endTime = TimeUtils.getTime(todayTimeInLong, TimeUtils.DATE_FORMAT_DATE) + " 23:59";
        return viewContent;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //设置输入框不可点击
        startTime_editText.setEnabled(false);
        endTime_editText.setEnabled(false);
        //清空开始时间与结束时间的editText
        startTime_editText.setText("");
        endTime_editText.setText("");
        switch (checkedId) {
            case today_rb:
                startTime = TimeUtils.getTime(todayTimeInLong, TimeUtils.DATE_FORMAT_DATE) + " 00:00";
                endTime = TimeUtils.getTime(todayTimeInLong, TimeUtils.DATE_FORMAT_DATE) + " 23:59";
                break;
            case yesterday_rb:
                startTime = TimeUtils.getTime(todayTimeInLong - 86400000, TimeUtils.DATE_FORMAT_DATE) + " 00:00";
                endTime = TimeUtils.getTime(todayTimeInLong - 86400000, TimeUtils.DATE_FORMAT_DATE) + " 23:59";
                break;
            case day_before_yesterday_rb:
                startTime = TimeUtils.getTime(todayTimeInLong - 86400000 * 2, TimeUtils.DATE_FORMAT_DATE) + " 00:00";
                endTime = TimeUtils.getTime(todayTimeInLong - 86400000 * 2, TimeUtils.DATE_FORMAT_DATE) + " 23:59";
                break;
            case user_defined_rb:
                //设置输入框可以点击
                startTime_editText.setEnabled(true);
                endTime_editText.setEnabled(true);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case start_time_et:
                //弹出时间选择器
                //设置是开始时间
                isStart = true;
                timePickerView.show();
                this.getDialog().hide();
                break;
            case end_time_et:
                //弹出时间选择器
                //设置不是开始时间
                isStart = false;
                timePickerView.show();
                this.getDialog().hide();
                break;
        }
    }

    /**
     * 获得开始时间
     */
    public String getStartTime() {
        return startTime;
    }

    /**
     * 获得结束时间
     */
    public String getEndTime() {
        return endTime;
    }
}
